/*
 */
package org.vectrics.recruiter.struts.tag;

import java.text.SimpleDateFormat;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.apache.ecs.html.A;
import org.vectrics.common.html.BorderTable;
import org.vectrics.common.html.Cell;
import org.vectrics.common.html.Form;
import org.vectrics.common.html.Row;
import org.vectrics.common.html.TextHelper;
import org.vectrics.common.pagelist.PageList;
import org.vectrics.common.util.StringUtils;
import org.vectrics.hibernate.ThreadProperties;
import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.recruiter.user.ResumeScanBatch;
import org.vectrics.recruiter.user.ResumeScanBatchCriteria;
import org.vectrics.struts.PageListTag;
import org.vectrics.user.Person;

/**
 * @author mmoore
 */
public class ResumeScanBatchListTag extends PageListTag {
    public static final long serialVersionUID = 132132424;

    private SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy hh:mm a");

    private String width = "500";

    private String jobId = null;

    @Override
    protected String getDescription() {
        // return(this.getPhrase("phrase.resume.scan.batch.list.desc"));
        return null;
    }

    @Override
    protected String getPageListLink() {
        return getContextPath() + "/admin/jobs/showResumeScanBatches.do";
    }

    @Override
    protected String getNoElementsMessage() {
        return this.getPhrase("phrase.resume.scan.batch.list.no.elements");
    }

    public String getJobId() {
        return jobId;
    }

    public void setJobId(final String jobId) {
        this.jobId = jobId;
    }

    /*
     *  
     */
    @Override
    protected void showList(final String description, final Cell mainCell, final List elements, final Form form) {
        ThreadProperties.forcedCommit();
        BorderTable table = new BorderTable();
        table.setWidth(width);
        mainCell.addElement(table);
        Row row = table.addRow();
        row.setDefaultBgImage(getContextPath() + "/site-graphics/tbl_hdr.gif");

        Cell cell = row.addCell();
        // cell.setClass("tableHdrBg");
        cell.addElement(TextHelper.getFont("tableHdrFont", this.getPhrase("phrase.resume.scan.batch.date.run")));
        cell = row.addCell();
        // cell.setClass("tableHdrBg");
        cell.addElement(TextHelper.getFont("tableHdrFont", this.getPhrase("phrase.resume.scan.batch.description")));
        cell = row.addCell();
        // cell.setClass("tableHdrBg");
        cell.addElement(TextHelper.getFont("tableHdrFont", this.getPhrase("phrase.resume.scan.batch.summary")));
        cell = row.addCell();
        // cell.setClass("tableHdrBg");
        cell.addElement(TextHelper.getFont("tableHdrFont", this.getPhrase("phrase.resume.scan.batch.status")));

        // Link
        cell = row.addCell();
        // cell.setClass("tableHdrBg");

        Iterator iter = elements.iterator();
        while (iter.hasNext()) {
            ResumeScanBatch batch = (ResumeScanBatch) iter.next();
            row = table.addRow();
            cell = row.addCell();
            cell.addElement(TextHelper.getFont("subFont", dateFormat.format(batch.getRunDate())));
            cell = row.addCell();
            cell.addElement(TextHelper.getFont("subFont", batch.getDescription()));
            cell = row.addCell();
            cell.addElement(TextHelper.getFont("subFont", batch.getMaxResumeAge().toString()));
            cell = row.addCell();
            cell.addElement(TextHelper.getFont("subFont", getStatusDescription(batch)));
            cell = row.addCell();

            A editLink = new A();
            if (StringUtils.isNullOrBlank(jobId) == true) {
                editLink.setHref(getContextPath() + "/admin/jobs/resumeScan.do" + "?batchId=" + batch.getId());
            } else {
                editLink.setHref(getContextPath() + "/admin/jobs/resumeScan.do" + "?batchId=" + batch.getId()
                        + "&jobId=" + jobId);
            }
            editLink.setClass("subLink");
            editLink.addElement(this.getPhrase("phrase.resume.scan.batch.status.run"));
            cell.addElement(editLink);
            cell.addElement(TextHelper.getFont("subFont", "&nbsp;"));
            A resultsLink = new A();
            if (StringUtils.isNullOrBlank(jobId) == false) {
                resultsLink.setHref(getContextPath() + "/admin/jobs/resumeScanResults.do" + "?batchId=" + batch.getId()
                        + "&jobId=" + getJobId());
            } else {
                resultsLink
                        .setHref(getContextPath() + "/admin/jobs/resumeScanResults.do" + "?batchId=" + batch.getId());
            }
            resultsLink.setClass("subLink");
            resultsLink.addElement(this.getPhrase("phrase.resume.scan.batch.status.view.results"));
            cell.addElement(resultsLink);
        }
    }

    public String getStatusDescription(final ResumeScanBatch batch) {
        int status = batch.getStatus();
        String desc = "Error - status not found";
        if (status == ResumeScanBatch.STATUS_FINISHED) {
            desc = this.getPhrase("phrase.resume.scan.batch.status.finished");
        } else if (status == ResumeScanBatch.STATUS_NOT_RUN) {
            desc = this.getPhrase("phrase.resume.scan.batch.status.not.run");
        } else if (status == ResumeScanBatch.STATUS_RUNNING) {
            desc = this.getPhrase("phrase.resume.scan.batch.status.running");
        }
        return desc;
    }

    /*
     * Create the page list used to populate this tag.
     */
    @Override
    protected PageList createPageList() {
        Person person = getAuthPerson();
        ResumeScanBatchCriteria criteria = new ResumeScanBatchCriteria();
        criteria.setRunPerson(person);
        Set domains = CoreServiceLocator.getPersonService().findDomainsWithUserPrivilege(person,
                ResumeScanBatchCriteria.PRIVILEGE_SCAN_BATCH_VIEW);
        criteria.setDomains(domains);
        return new ResumeScanBatchPageList(criteria);
    }

    /**
     * @return Returns the width.
     */
    @Override
    public String getWidth() {
        return width;
    }

    /**
     * @param width
     *            The width to set.
     */
    @Override
    public void setWidth(final String width) {
        this.width = width;
    }
}
